var pageSize = 20;
var pageStart = 1;
var pageCount = 1;
$(function(){
	loadInfo();
	//删除按钮事件
	$("tbody").on('click','.del',function(){
		var id = $(this).attr("name");
		bootbox.confirm("您确认执行该操作？",function(result){
			if(result){
				delArticle(id);
			}
		});
	});
	//publish按钮事件
	$("tbody").on('click','.publish',function(){
		var id = $(this).attr("name");
		publishArticle(id);
	});
	//redo按钮事件
	$("tbody").on('click','.redo',function(){
		var id = $(this).attr("name");
		redoArticle(id);
	});
	//推荐按钮事件
	$("tbody").on('click','.recommend',function(){
		var id = $(this).attr("name");
		recommendArticle(id);
	});
	//推荐到首页按钮事件
	$("tbody").on('click','.index',function(){
		var id = $(this).attr("name");
		indexArticle(id);
	});
	//上一页
	$("#prev").on('click',function(){
		if(pageStart>1){
			pageStart--;
			loadInfo();
		}
	});
	//下一页
	$("#next").on('click',function(){
		if(pageStart<pageCount){
			pageStart++;
			loadInfo();
		}
	});
	//跳转
	$("#go").on('click',function(){
		var page = $("#pageNum").val();
		var regx = /^[0-9]+$/;
		if(regx.test(page)){
			var pageNum = Number(page);
			if(pageNum>0&&pageNum<=pageCount){
				pageStart = pageNum;
				loadInfo();
			}
		}
	});
});

function indexArticle(id){
	$.ajax({
		url:BASE_PATH+'/article/showOnIndex.do',
		type:'post',
		dataType:'json',
		data:{
			id:id,
			showOnIndex:1
		},
		success:function(data){
			if(data.status=='success'){
				$(".index[name='"+id+"']").remove();
			}else{
				if(data.errorMsg=='expire'){
					window.location.href="login.html"
				}else{
					alert(data.erroeMsg);
				}
			}
		}
	});
}

function recommendArticle(id){
	$.ajax({
		url:BASE_PATH+'/article/recommend.do',
		type:'post',
		dataType:'json',
		data:{
			id:id,
			recommend:1
		},
		success:function(data){
			if(data.status=='success'){
				$(".recommend[name='"+id+"']").remove();
			}else{
				if(data.errorMsg=='expire'){
					window.location.href="login.html"
				}else{
					alert(data.erroeMsg);
				}
			}
		}
	});
}

function delArticle(id){
	$.ajax({
		url:BASE_PATH+'/article/del.do',
		type:'get',
		dataType:'json',
		data:{
			id:id
		},
		success:function(data){
			if(data.status=='success'){
				$(".del[name='"+id+"']").parents('tr').remove();
			}else{
				if(data.errorMsg=='expire'){
					window.location.href="login.html"
				}else{
					alert(data.erroeMsg);
				}
			}
		}
	});
}

function publishArticle(id){
	$.ajax({
		url:BASE_PATH+'/article/publish.do',
		type:'post',
		dataType:'json',
		data:{
			id:id,
			draft:2
		},
		success:function(data){
			if(data.status=='success'){
				$(".publish[name='"+id+"']").children('i').removeClass("icon-upload").addClass('icon-download');
				$(".publish[name='"+id+"']").parents('td').prev().html('已发布');
				$(".publish[name='"+id+"']").removeClass("publish").addClass('redo');
				$(".publish[name='"+id+"']").attr('title','撤回');
				$(".edit[name='"+id+"']").hide();
			}else{
				if(data.errorMsg=='expire'){
					window.location.href="login.html"
				}else{
					alert(data.erroeMsg);
				}
			}
		}
	});
}

function redoArticle(id){
	$.ajax({
		url:BASE_PATH+'/article/publish.do',
		type:'post',
		dataType:'json',
		data:{
			id:id,
			draft:1
		},
		success:function(data){
			if(data.status=='success'){
				$(".redo[name='"+id+"']").children('i').removeClass("icon-download").addClass('icon-upload');
				$(".redo[name='"+id+"']").parents('td').prev().html('草稿');
				$(".redo[name='"+id+"']").removeClass("redo").addClass('publish');
				$(".publish[name='"+id+"']").attr('title','发布');
				$(".edit[name='"+id+"']").show();
			}else{
				if(data.errorMsg=='expire'){
					window.location.href="login.html"
				}else{
					alert(data.erroeMsg);
				}
			}
		}
	});
}

function loadInfo(){
	$.ajax({
		url:BASE_PATH+'/article/list.do',
		type:'get',
		dataType : 'json',
		data:{pageSize:pageSize,pageStart:pageStart},
		success : function(data) {
			if(data.errorMsg == 'expire'){
				window.location.href="login.html";
			}else{
				createRows(data);
				createPaginator(data);
				pageCount = data.pageCount;
				pageStart = data.page;
			}
		}
	});
}

function createPaginator(data){
	if(data.pageCount<=1){
		//总共一页
		$(".pageinfo").hide();
	}else{
		$(".pageinfo").show();
		$("#pageInfo").html("第"+data.page+"页/共"+data.pageCount+"页");
	}
}

function createRows(data){
	var html = "";
	if(data.article){
		for(var i = 0; i < data.article.length; i++){
			html+="<tr>"
				+	"<td>"+data.article[i].id+"</td>"
				+	"<td><a target='_blank' href='"+BASE_PATH+"/article/"+data.article[i].id+".html'>"+data.article[i].title+"</a></td>"
				+	"<td>"+data.navbar[data.article[i].navbar].title+"</td>"
				+	"<td>"+new Date(data.article[i].createTime).pattern("yyyy-MM-dd")+"</td>"
				+	"<td>"+new Date(data.article[i].updateTime).pattern("yyyy-MM-dd")+"</td>"
				+	"<td>"+getTagTd(data.article[i].tag,data.tag)+"</td>"
				+	"<td>"+(data.article[i].draft==1?"草稿":"已发布")+"</td>"
				+"<td>"
				 +"<div class=\"action-buttons\">"
				+		"<a class=\"green edit\" name=\""+data.article[i].id+"\" href=\"addarticle.html?id="+data.article[i].id+"\" style=\""+(data.article[i].draft==2?"display:none":"")+"\">"
				+			"<i class=\"icon-pencil bigger-130\"></i>"
				+		"</a>"
				+		"<a class=\"red del\" href=\"#\" name=\""+data.article[i].id+"\">"
				+			"<i class=\"icon-trash bigger-130\"></i>"
				+		"</a>"
				+		"<a title=\""+(data.article[i].draft==1?"发布":"撤回")+"\" class=\"green "+(data.article[i].draft==1?"publish":"redo")+"\" href=\"#\" name=\""+data.article[i].id+"\">"
				+			"<i class=\""+(data.article[i].draft==1?"icon-upload":"icon-download")+" bigger-130\"></i>"
				+		"</a>";
			if(data.article[i].recommend==2){
				html+=	"<a title='推荐' class=\"blue recommend\" href=\"#\" name=\""+data.article[i].id+"\">"
				+			"<i class=\"icon-thumbs-up-alt bigger-130\"></i>"
				+		"</a>";
			}
			if(data.article[i].showOnIndex==2){
				html+=	"<a title='首页推荐' class=\"purple index\" href=\"#\" name=\""+data.article[i].id+"\">"
				+			"<i class=\"icon-upload-alt bigger-130\"></i>"
				+		"</a>";
			}	
				
				html+=	"</div>"
				 +"</td>"
				+"</tr>";
		}
	}
	$("tbody").html(html);
}

function getTagTd(tagstr,tagmap){
	var html = "";
	var array = tagstr.split(",");
	for(var i=0;i<array.length;i++){
		if(tagmap[array[i]]){
			html+="<span class='label' style='background-color:"+tagmap[array[i]].tagColor+"!important;margin-right:2px;'>"+tagmap[array[i]].tagName+"</span>";
		}
	}
	return html;
}